package com.dh.service.impl;

import com.dh.core.config.Constant;
import com.dh.core.config.UserRole;
import com.dh.entity.User;
import com.dh.repository.UserRepository;
import com.dh.service.UserGoodService;
import com.dh.service.UserRPService;
import com.dh.service.UserService;
import com.dh.service.UserTaxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private UserGoodService userGoodService;

    @Autowired
    private UserRPService rpService;

    @Autowired
    private UserTaxService taxService;

    public Page<User> page(PageRequest pageRequest, String nickname) {
        if (StringUtils.isEmpty(nickname)) {
            return userRepository.findByDeletedAndUserType(Constant.NOT_DELETE, Constant.USER_TYPE_DY, pageRequest);
        } else {
            return userRepository.findByDeletedAndUserTypeAndNicknameContaining(Constant.NOT_DELETE, Constant.USER_TYPE_DY, nickname, pageRequest);
        }
    }

    @Override
    public User get(Long userId) {
        return userRepository.findOne(userId);
    }


    public void delete(Long userId) {
        //        User account = userRepository.findOne(userId);
        //        account.setDeleted(Constant.DELETE);
        //        userRepository.save(account);
        userRepository.delete(userId);
        //同时删除优秀党员
        userGoodService.deleteByUserId(userId);
        rpService.deleteByUserId(userId);
        taxService.deleteByUserId(userId);
    }

    @Override
    public void create(User user) {
        userRepository.save(user);
    }

    @Override
    public void update(User user) {
        userRepository.save(user);
    }

    @Override
    public List<User> findAllDy() {
        List<User> list = userRepository.findByDeletedAndUserType(Constant.NOT_DELETE, Constant.USER_TYPE_DY);
        return list.stream().filter(u -> 1 != u.getAdmin()).filter(u -> UserRole.NORMARL.getId()
            == u.getAccountRole()).collect(Collectors.toList());
    }

    public Page<User> systemUser(PageRequest pageRequest, String nickname) {
        if (StringUtils.isEmpty(nickname)) {
            return userRepository.findByDeletedAndUserType(Constant.NOT_DELETE, Constant.USER_TYPE_USER, pageRequest);
        } else {
            return userRepository.findByDeletedAndUserTypeAndNicknameContaining(Constant.NOT_DELETE, Constant.USER_TYPE_USER, nickname, pageRequest);
        }
    }


    public User findByUsername(String username) {
        return userRepository.findByUsernameAndDeleted(username, Constant.NOT_DELETE);
    }

    public User findByNoAndDeleted(String no) {
        return null;
    }
}
